package com.example.websockettest.webSocket.MyTool;

import com.alibaba.fastjson.JSONObject;
import com.example.websockettest.webSocket.entity.Wenzhang;
import com.example.websockettest.webSocket.utils.通用操作数据库;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import static com.example.websockettest.webSocket.ProxyPost.ProxyExample.response;
@Component
public class TestTOOL extends 通用操作数据库 {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        while (true) {

            System.out.println("请输入：");
            String a=input.nextLine();

            String  res = null;
            try {
                res = response(a);
            } catch (IOException e) {
                 System.out.println("出现异常");
            }
            System.out.println(res);
        }
        //searchFiles("C:\\Users\\Administrator\\Desktop\\最近的任务\\GPT情景训练");



    }

    @PostConstruct
    public void test() throws IOException {

        Thread thread = new Thread() {
            @Override
            public void run() {
                System.out.println("----------------读取文件内容--------------");
                searchFiles("C:\\Users\\Administrator\\Desktop\\最近的任务\\GPT情景训练");
            }

        };
        //thread.start();
    }
    private static void readDetails(String path,String fileName,String folderName) {
        try {
            File file = new File(path);
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            int i=0,shunid=0;
            Wenzhang wenzhang;
            List<String> users= Arrays.asList("You", "Interviewee");
            String user="You";
            String assistant = "assistant";
            while ((line = br.readLine()) != null) {
                // 处理每一行内容
                if(line.equals("")){//过滤换行
                    //System.out.println("换行");
                    continue;
                }
                if(users.contains(line.split(":")[0])){
                    user=line;
                }else {
                    assistant=line;
                }

                //System.out.print(line);
                if(i!=0&&i%2==0){
                    wenzhang=new Wenzhang(user,assistant, (long) shunid,fileName,folderName);
                    wenzhangMapper.insertWenzhang(wenzhang);
                    System.out.println(JSONObject.toJSONString(wenzhang));
                    shunid++;
                }
                i++;
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void searchFiles(String folderPath) {//文件路径
        File folder = new File(folderPath);
        File[] files = folder.listFiles(); // 获取文件夹下的所有文件和文件夹
        for (File file : files) {
            if (file.isFile() && file.getName().endsWith(".txt")) { // 如果是txt文件
                String fileName=file.getName().replace(".txt","");
                readDetails(file.getPath(),fileName,folder.getName());
                //System.out.println("父级文件夹名：" + folder.getName());
                //System.out.println("文件名：" + file.getName());
            } else if (file.isDirectory()) { // 如果是文件夹
                searchFiles(file.getAbsolutePath()); // 递归搜索
            }
        }
    }
}
